# -*- coding: utf-8 -*-
from datetime import datetime
from app.exts import db # ,login_manager
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
# from sqlalchemy.orm import Mapped,mapped_column

# __all__ = ['Todo']


# @login_manager.user_loader
# def load_user(user_id):
#     return User.query.get(int(user_id))


    
class TodoModel(db.Model):
    """data model"""
    __tablename__ = 'todos'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(255), nullable=False)
    posted_on = db.Column(db.DateTime, default=datetime.now)
    status = db.Column(db.Boolean(), default=False)

    def __init__(self, *args, **kwargs):
        super(TodoModel, self).__init__(*args, **kwargs)

    def __repr__(self):
        return "<Todo '%s'>" % self.title

    def store_to_db(self):
        """save to database"""

        db.session.add(self)
        db.session.commit()

    def delete_todo(self):
        """delete data"""
        db.session.delete(self)
        db.session.commit()
    
class UserMovieModel(db.Model, UserMixin):
    __tablename__ = 'movieusers'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    username = db.Column(db.String(20))
    password_hash = db.Column(db.String(128))

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)
    
class MovieModel(db.Model):  # 表名将会是 movie
    __tablename__ = 'movies'
    id = db.Column(db.Integer, primary_key=True)  # 主键
    title = db.Column(db.String(60))  # 电影标题
    year = db.Column(db.String(4))  # 电影年份
    
class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64),  unique=True)  # 11位手机号 或者 手机号
    password = db.Column(db.String(256),nullable=False)    # 存储的密码经过hash加密的
    
    def __repr__(self) -> str:
        return f'<User {self.username}>'
    
    @property
    def password(self):
        raise AttributeError('密码字段不能读取')
    
    @password.setter
    def password(self, password):
        # 当类初始化时自定调用，把password的值转换为散列值存储在password_hash字段中
        self.password_hash = generate_password_hash(password)
    
    def verify_password(self, password) -> bool:
        # 用于密码登录时验证密码
        return check_password_hash(self.password_hash, password)

class WeixinUser(db.Model):
    # id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    __tablename__ = 'weixin_users'
    
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    openid = db.Column(db.String(64),  unique=True,nullable=True)
    phone = db.Column(db.String(11), unique=True,nullable=False)
    # id:Mapped[int] = mapped_column(primary_key=True,autoincrement=True)
    # openid:Mapped[StringField] = mapped_column(StringField,unique=True)
    # username:Mapped[StringField]  = mapped_column(StringField)
    # phone:Mapped[int] = mapped_column(StringField,unique=True)
    
    
